package eerbaar.v2.app.view2;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.app.tasks.IGameTask.Priority;
import eerbaar.v2.app.view.IView;
import eerbaar.v2.app.view.Squares;

public class PersonaView implements IView {

    private final List<PersonaDisplayer> displayers = new ArrayList<PersonaDisplayer>();
    private final ImageStore store = new ImageStore();
    private final IView inner;

    public PersonaView(IView inner) {
        this.inner = inner;
    }

    private void createDisplayers() {

        IGame game = GameHelper.getGame();
        if (game == null) {
            return;
        }

        List<IPersona> list = new ArrayList<IPersona>();

        IPersona[] personas = game.getPersonas();
        for (IPersona p : personas) {
            PersonaDisplayer d = new PersonaDisplayer(this.store, p);
            if (!this.displayers.contains(d)) {
                this.displayers.add(d);
            }
            list.add(p);
        }

        List<PersonaDisplayer> remove = new ArrayList<PersonaDisplayer>();
        for (PersonaDisplayer d : this.displayers) {
            IPersona p = d.getPersona();
            if (!list.contains(p)) {
                remove.add(d);
            }
        }

        for (PersonaDisplayer d : remove) {
            this.displayers.remove(d);
        }

    }

    @Override
    public void draw(Graphics2D g) {

        createDisplayers();

        for (PersonaDisplayer d : this.displayers) {
            d.draw(g);
        }
    }

    @Override
    public int getHeight() {
        return this.inner.getHeight();
    }

    @Override
    public Priority getPriority() {
        return Priority.LAST;
    }

    @Override
    public int getSquareHeight() {
        return this.inner.getSquareHeight();
    }

    @Override
    public Squares getSquares() {
        return this.inner.getSquares();
    }

    @Override
    public int getSquareWidth() {
        return this.inner.getSquareWidth();
    }

    @Override
    public int getWidth() {
        return this.inner.getWidth();
    }

}
